/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.doeacc.tms.web.bean;

import java.io.Serializable;
import java.util.List;
import java.util.ResourceBundle;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;

import edu.doeacc.tms.domain.Customer;
import edu.doeacc.tms.service.dao.CustomerFacade;
import edu.doeacc.tms.web.util.JsfUtil;

/**
 *
 * @author slok
 */
@ManagedBean(name = "customerBean")
@ViewScoped
public class CustomerBean implements Serializable {

    @EJB
    private transient  CustomerFacade customerFacade;
    private List<Customer> customers;
    private Customer current;

    @ManagedProperty("#{msgs}")
    private transient  ResourceBundle bundle;

    public void setBundle(ResourceBundle bundle) {
        this.bundle = bundle;
    }

    public CustomerFacade getCustomerFacade() {
        return customerFacade;
    }

    public void setCustomerFacade(CustomerFacade customerFacade) {
        this.customerFacade = customerFacade;
    }

    public List<Customer> getCustomers() {
        if (this.customers == null) {
            this.customers = customerFacade.findAll();
        }
        return customers;
    }

    public void setCustomers(List<Customer> customers) {
        this.customers = customers;
    }

    public Customer getSelected() {
         if (current == null) {
            if (this.currentCustomerId > -1) {
                this.current = customerFacade.find(this.currentCustomerId);
            } else {
                current = new Customer();
            }          
        }
        return current;
    }

    public String prepareEdit(Long id) {
        this.current = customerFacade.find(id);
        return "pretty:editCustomer";
    }
    public String update() {
        try {
            customerFacade.edit(current);
            JsfUtil.addSuccessMessage("Customer Updated Successfully");
            return "pretty:listCustomers";
        } catch (Exception e) {
            JsfUtil.addErrorMessage(e, bundle.getString("PersistenceErrorOccured"));
            return null;
        }
    }


     public String prepareCreate() {
        this.current = new Customer();
        return "pretty:addCustomer";
    }

      public String create() {
        try {
           customerFacade.create(current);
            JsfUtil.addSuccessMessage("New Customer Created");
            return "pretty:listCustomers";
        } catch (Exception e) {
            JsfUtil.addErrorMessage(e, bundle.getString("PersistenceErrorOccured"));
            return null;
        }
    }
    public String destroy(Long id) {
         try {
            customerFacade.remove(customerFacade.find(id));
            customers = null;
            JsfUtil.addSuccessMessage("Customer deleted successfully");
        } catch (Exception e) {
            JsfUtil.addErrorMessage(e, bundle.getString("PersistenceErrorOccured"));
        }
        return "pretty:listCustomers";
    }

    Long currentCustomerId = -1L;

    public Long getCurrentCustomerId() {
        return getSelected().getId();
    }

    public void setCurrentCustomerId(Long id) {
        this.currentCustomerId = id;
    }

    
}
